﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using blackhouse.Data;

namespace blackhouse.BlackApart.Data.Sql {
    public class AdditionalServicesDataAccess : AdoAdditionalServicesDataAccess {

        #region Fields

        private string connectionString;
        private MsSqlAdoDataHelper dataHelper;

        #endregion

        #region Constructors

        public AdditionalServicesDataAccess(string sqlConnectionString) {
            this.connectionString = sqlConnectionString;
            this.dataHelper = new MsSqlAdoDataHelper(this.connectionString);
            SqlVersioning versioning = new SqlVersioning(this.connectionString, this.GetType().Assembly);
        }

        #endregion

        #region Methods

        protected override System.Data.IDataReader GetAvalibleServicesReader(int apartmentGroupId, string language) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_AdditionalServicesGetByApartment";
            this.dataHelper.AddParameter("ApartmentGroupId", apartmentGroupId, System.Data.DbType.Int32);
            this.dataHelper.AddParameter("Language", language, System.Data.DbType.StringFixedLength);
            return this.dataHelper.Execute();
        }

        protected override System.Data.IDataReader GetAvalibleServicesForReservationReader(int forReservationId, string language) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_AdditionalServicesGetByReservation";
            this.dataHelper.AddParameter("ReservationId", forReservationId, System.Data.DbType.Int32);
            this.dataHelper.AddParameter("Language", language, System.Data.DbType.StringFixedLength);
            return this.dataHelper.Execute();
        }

        public override int AddServiceToReservation(int reservationId, int serviceId, double price, string clientReview) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_AdditionalServicesAddToReservation";
            this.dataHelper.AddParameter("ReservationId", reservationId, System.Data.DbType.Int32);
            this.dataHelper.AddParameter("ServiceId", serviceId, System.Data.DbType.Int32);
            this.dataHelper.AddParameter("Price", price, System.Data.DbType.Double);
            if (!string.IsNullOrEmpty(clientReview))
                this.dataHelper.AddParameter("ClientReview", clientReview, System.Data.DbType.String);
            return Convert.ToInt32(this.dataHelper.ExecuteScalar());
        }

        public override void AddServiceItemToReservation(int orderId, DateTime date, int quantity, double price) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_AdditionalServicesAddLine";
            this.dataHelper.AddParameter("OrderId", orderId, System.Data.DbType.Int32);
            this.dataHelper.AddParameter("Date", date, System.Data.DbType.DateTime);
            this.dataHelper.AddParameter("Quantity", quantity, System.Data.DbType.Int32);
            this.dataHelper.AddParameter("Price", price, System.Data.DbType.Double);
            this.dataHelper.ExecuteWithoutResult();
        }

        protected override IDataReader GetReservationServiceReader(int reservationId, int serviceId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_AdditionalServicesGetOrder";
            this.dataHelper.AddParameter("ReservationId", reservationId, System.Data.DbType.Int32);
            this.dataHelper.AddParameter("ServiceId", serviceId, System.Data.DbType.Int32);
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetServiceItemFromOrderReader(int orderId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_AdditionalServicesGetLines";
            this.dataHelper.AddParameter("OrderId", orderId, System.Data.DbType.Int32);
            return this.dataHelper.Execute();
        }

        #endregion

    }
}
